import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'package:open_file/open_file.dart';
import 'package:path/path.dart';
import '../../helper/channelHelper.dart';
import '../../helper/Helper.dart';

import 'package:photo_view/photo_view.dart';
import '../../model/recording.dart';

class RecordImageScanPage extends StatefulWidget {
  @override
  RecordImageScanPage(this.Img1);

  final Recording Img1;

  @override
  RecordImageScanState createState() => new RecordImageScanState();
}

class RecordImageScanState extends State<RecordImageScanPage> {
  late File _img;
  bool Loading = true;

  Future rotateImg() async {
    var img2 = await Helper.ImgRotate(_img);
    setState(() {
      Loading = false;
      _img = img2;
    });
  }

  Future<String> getFilePickPath() async {
    var result = await FilePicker.platform.pickFiles();

    if (result != null) {
      return result.files.single.path!;
    } else {
      return "";
    }
  }

  Future saveImg() async {
    var f1 = await getFilePickPath();

    String x2 = dirname(f1);
    String x1 = basename(_img.path);
    String x3 = x2 + '/' + x1.replaceAll("-", "_");
    if (File(x3).existsSync()) File(x3).deleteSync();
    var img2 = _img.copySync(x3);
    if (img2.existsSync()) {
      setState(() {
        Loading = false;
        _img = img2;
      });
    }
  }

  Future saveImg2() async {
    await Helper.GetstoragePermission();

    final result = await ChannelHelper.saveFile(_img.path);
    await ChannelHelper.showToast(result);
  }

  Future saveImg3() async {
    await Helper.GetstoragePermission();
    var p1 = await getFilePickPath();
    final result = await ChannelHelper.saveFile(p1);
    print(result);
  }

  Future getImgFile() async {
    var file1 = await Helper.fileCache
        .getSingleFile(Helper.GetImgUrl(widget.Img1.ImgId));

    if (!mounted) return;
    setState(() {
      _img = file1;
      Loading = false;
    });
  }

  @override
  initState() {
    super.initState();
    getImgFile();
  }

  @override
  void dispose() {
    //注意这里关闭
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return new Scaffold(
      appBar: AppBar(
        title: Text("图片查看"),
      ),
      persistentFooterButtons: <Widget>[
        ElevatedButton(
          child: Text(
            "下载",
            style: TextStyle(color: Colors.black54, fontSize: 16.0),
          ),
          onPressed: () {
            saveImg2();
          },
        ),
        ElevatedButton(
          child: Text(
            "旋转",
            style: TextStyle(color: Colors.black54, fontSize: 16.0),
          ),
          onPressed: () {
            rotateImg();
          },
        ),
        ElevatedButton(
          child: Text("打开", style: TextStyle(fontSize: 16.0)),
          onPressed: () {
            OpenFile.open(_img.path);
          },
        ),
      ],
      body: Loading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : new Container(
              child: _img != null
                  ? PhotoView(
                      imageProvider: FileImage(_img),
                      //  backgroundColor: GlobalConfig.cardBackgroundColor,
                    )
                  : Center(
                      child: Text("未选择图片！"),
                    ),
            ),
    );
  }
}
